package dk.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ReturnData.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import dk.Service.TokenService;
import dk.beans.Menu;
import dk.mapper.menumapper;

@RestController
public class menucontroller {
	@Autowired
	TokenService tokenservice;
	@Autowired
	menumapper menumapper;
	@RequestMapping("/GetMenu")
	public ResponseData GetMenu(String token) {
		//去数据库查id
		ResponseData data = new ResponseData();
		Integer id = tokenservice.GetIdByToken(token);
		System.out.println("/GetMenu开始运行");
		System.out.println(id);
		if(id>0) {
			
			//可以查询到 先寻找pid = 0
			QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("pid",0);
			List<Menu> Menulist = menumapper.selectList(queryWrapper);
			for(Menu menu:Menulist) {
				//遍历取出所有pid = 0的菜单 找到他们的id 再根据用这个id作为pid查询他们的子类
				QueryWrapper<Menu> queryWrapper2 = new QueryWrapper<>();
				queryWrapper2.eq("pid",menu.getId());
				menu.setChildren(menumapper.selectList(queryWrapper2));
			}
			for(Menu menu:Menulist) {
				System.out.println(menu);
			}
			data.setCode(200);
			data.setMessage("获取菜单成功");
			data.setData(Menulist);
			return data;
			
	
		
		}
		data.setCode(201);
		data.setMessage("获取菜单失败");
		
		
		
		
		return data;
	}

}
